package com.example.yunhe_app;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private final static int CAPTURE_IMAGE_REQUEST_CODE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        Button camera=(Button)findViewById(R.id.openCamera);
        camera.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
//        		Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//				File mediaStorageDir=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
//				if(!mediaStorageDir.exists())
//				{
//					mediaStorageDir.mkdirs();
//				}
//				String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0));
//				String mDisplayFolder="Pictures"+File.separator+"test"+File.separator+"IMG_"+timeStamp+".jpg";
//				File mPhotoFile=new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");
//				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
//				startActivityForResult(intent,CAPTURE_IMAGE_REQUEST_CODE);
        		Intent intent=new Intent(MainActivity.this,PhotoActivity.class);
        		startActivity(intent);
        	}
        });
        
        Button picture=(Button)findViewById(R.id.openAlbum);
        picture.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		Intent intent = new Intent();   
                intent.setType("image/*");   
                intent.setAction(Intent.ACTION_GET_CONTENT);   
                startActivityForResult(intent, 1);  
        	}
        });
    }
    
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            Uri uri = data.getData();  
            Log.e("uri", uri.toString());  
            ContentResolver cr = this.getContentResolver();  
            try {  
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));  
                ImageView imageView = (ImageView) findViewById(R.id.iv01);  
                imageView.setImageBitmap(bitmap);  
                imageView.setVisibility(View.VISIBLE);
            } catch (FileNotFoundException e) {  
                Log.e("Exception", e.getMessage(),e);  
            }  
        }  
        super.onActivityResult(requestCode, resultCode, data);  
    }  
}
